//
//  SettingViewController.m
//  51易借
//
//  Created by 张睿 on 2017/8/28.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "SettingViewController.h"
#import "SettingTableViewCell.h"
#import "ChangePasswordController.h"
#import "AboutMeViewController.h"
#import "LoginViewController.h"
#import "AppDelegate.h"
#import "OrangeNavController.h"
#import "UIWebViewController.h"

@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UIView * topView;
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, weak)UIButton * logoutButton;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BACK_COLOR;
    
    [self setupNavBar];
    [self setupUI];
}


- (void)setupNavBar{
    
    self.title = @"设置";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToLastView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}


- (void)setupUI{
    
    UIView * topView = [UIView new];
    
    topView.backgroundColor = TOTAL_COLOR;
    
    self.topView = topView;
    
    [self.view addSubview:self.topView];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(5 * SCREENH6, 75 * SCREENH6, SCREEN_WIDTH - 10 * SCREENH6, 204 * SCREENH6)];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.bounces = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[SettingTableViewCell class] forCellReuseIdentifier:@"settingCell"];
    
    [self.view addSubview:self.tableView];
    
    
    UIButton * logoutButton = [UIButton new];
    //300 * 42
    [logoutButton setTitle:@"退出登录" forState:UIControlStateNormal];
    [logoutButton setBackgroundImage:[UIImage imageNamed:@"登录"] forState:UIControlStateNormal];
    [logoutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [logoutButton addTarget:self action:@selector(didLogOutButton) forControlEvents:UIControlEventTouchUpInside];
    logoutButton.titleLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    
    self.logoutButton = logoutButton;
    
    [self.view addSubview:self.logoutButton];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 4;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"settingCell" forIndexPath:indexPath];
    
    if (indexPath.row == 0) {
        
        cell.kindLabel.text = @"关于我们";
        cell.rightLabel.hidden = YES;
    }else if (indexPath.row == 1){
        
        cell.kindLabel.text = @"微信公众号";
        cell.rightImageView.hidden = YES;
        cell.rightLabel.text = @"xianjinedai";
    }else if (indexPath.row == 2){
        
        cell.kindLabel.text = @"版本";
        cell.rightImageView.hidden = YES;
        cell.rightLabel.text = @"V1.0";
        cell.rightLabel.userInteractionEnabled = NO;
    }else{
        
        cell.kindLabel.text = @"修改登录密码";
        cell.rightLabel.hidden = YES;
    }
    
    
    return cell;
    
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 51 * SCREENH6;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.hidesBottomBarWhenPushed = YES;
    
    if (indexPath.row == 3) {
        
        ChangePasswordController * changeVC = [ChangePasswordController new];
        
        [self.navigationController pushViewController:changeVC animated:YES];
        
    }else if(indexPath.row == 0){
        
//        AboutMeViewController * aboutVC = [AboutMeViewController new];
//        
//        [self.navigationController pushViewController:aboutVC animated:YES];
        
        UIWebViewController * webVC = [UIWebViewController new];
        
        webVC.count = @"3";
        
        [self.navigationController pushViewController:webVC animated:YES];
    }
}


- (void)viewDidLayoutSubviews{
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(64);
    }];
    
    
    [self.logoutButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.tableView.mas_bottom).offset(84 * SCREENH6);
        make.width.mas_equalTo(300 * SCREENH6);
        make.height.mas_equalTo(42 * SCREENH6);
    }];
}



- (void)didLogOutButton{
//    DEF_SET_OBJECT(nil, userToken);
//    DEF_SET_OBJECT(nil, userID);
//    DEF_SET_OBJECT(nil, myAccount);
    NSUserDefaults *defatluts = [NSUserDefaults standardUserDefaults];
    
    NSDictionary *dictionary = [defatluts dictionaryRepresentation];
    
    for(NSString *key in [dictionary allKeys]){
        
        [defatluts removeObjectForKey:key];
        [defatluts synchronize];
    }
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    LoginViewController * loginVC = [LoginViewController new];
    
    loginVC.typeString = @"1";
    
    OrangeNavController * navVC = [[OrangeNavController alloc] initWithRootViewController:loginVC];
    
    appDelegate.window.rootViewController = navVC;
}



- (void)backToLastView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
